﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Odyssey.Linq;
using SunTzu.Web;
using System.IO;
using Rea.Web.Models;
using SunTzu.Core.Data;
using Odyssey.Web.Models;

namespace Odyssey.Web.Controllers
{
    [Login]
    public class ReferralApplyController : FrontBaseController
    {
        public ActionResult New()
        {
            ViewData.Model = db.form_referral_applies.FirstOrDefault(c => c.AccountId == LoginHelper.CurrentUserId)
                ?? new form_referral_apply();
            return View();
        }

        [HttpPost]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult CreateReferralApply(int? id)
        {
            form_referral_apply apply = db.form_referral_applies.FirstOrDefault(c => c.AccountId == LoginHelper.CurrentUserId)
                ?? new form_referral_apply();
            HTMLHelper.BindModel(apply);
            apply.AccountId = LoginHelper.CurrentUserId;
            if (apply.SaveOrUpdate())
            {
                SetMessage("保存成功！");
            }

            return RedirectToAction("New");
        }

        public ActionResult Detail(int id)
        {
            form_referral_apply entity = db.form_referral_applies.FirstOrDefault(c => c.Id == id);
            ViewData.Model = entity ?? new form_referral_apply();
            BindMasterViewData(2);
            return View();
        }

        public ActionResult List(int? page)
        {
            PagedList<form_referral_apply> referralList = null;
            //if (subcategory.HasValue)
            //{
            //var sub = subcategory.ToString();
            referralList = db.form_referral_applies
                .Where(c => c.State == 1)
                .ToPagedList(page.GetValueOrDefault());

            //}
            ViewBag.Referral = referralList ?? new List<form_referral_apply>().ToPagedList(page.GetValueOrDefault());
            BindMasterViewData(2);
            return View();
        }
    }
}
